/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import db.StorageItemDb;
import db.StorageItemInterface;
import java.util.ArrayList;
import java.util.HashMap;
import models.Storage;
import models.StorageItem;

/**
 *
 * @author Dan
 */
public class StorageItemController {

    private StorageItemInterface stoItemDb = new StorageItemDb();
    private ArrayList<StorageItem> storageItemList = new ArrayList<StorageItem>();
    private StorageItem temporaryStorageItemChangeObjet = null;
     private Storage temporaryStorageChangeObjet = null;
    private StorageController stoCtr = new StorageController();
    
    public StorageItem buildItem(int storageId, int amount, int lostAmount, String serialnumber) {
        StorageItem stoItem = new StorageItem(storageId, amount, lostAmount, serialnumber);
        return stoItem;
    }

    public ArrayList<StorageItem> getAllStorageItem() {
        return storageItemList = stoItemDb.getAllRentabelItems();
    }

    public ArrayList<StorageItem> getStorageItems(int Id) {
        return storageItemList = stoItemDb.getRentabelItems(Id);
    }

    public ArrayList<StorageItem> searchStorageItem(String name, String serienummer) {
        return storageItemList = stoItemDb.searchStorageItem(name, serienummer);
    }

    public int addStorageItem(ArrayList si, int id) {
        storageItemList = si;
        for (StorageItem sItem : storageItemList) {
            sItem.setStorageId(id);
        }
        return stoItemDb.addIteam(si);
    }

    public boolean updateStorage(ArrayList l) {
        ArrayList<StorageItem> si = l;
        ArrayList<StorageItem> addItem = new ArrayList<StorageItem>();
        boolean allsucceed = false;
        int succeedUpdate = 0;
        int succeedAdd = 0;
        for (StorageItem item : si) {
            if (item.getStorageId() > 0) {
                if (stoItemDb.editItem(item)) {
                    succeedUpdate++;
                }
            } else {
                
                item.setStorageId(temporaryStorageChangeObjet.getId());
                addItem.add(item);
            }
        }
        if (addItem.size() > 0) {
            succeedAdd = stoItemDb.addIteam(addItem);
        }
        if (succeedAdd > 0 || succeedUpdate == si.size() - addItem.size()) {
            allsucceed = true;
        }
        return allsucceed;
    }

    public int getExsempelAmount(int id) {
        String stoid = String.valueOf(id);
        int exsempelAmount = 0;
        HashMap<String, String> exsempelList = stoItemDb.getExempelAmount();
        if (exsempelList.get(stoid) != null) {
            exsempelAmount = Integer.valueOf(exsempelList.get(stoid));

        }

        return exsempelAmount;
    }
    public void setTemporaryStorageItemChangeObjet(StorageItem temporaryStorageItemChangeObjet) {
        this.temporaryStorageItemChangeObjet = temporaryStorageItemChangeObjet;
    }

    public StorageItem getTemporaryStorageItemChangeObjet() {
        return temporaryStorageItemChangeObjet;
    }
     public void setTemporaryStorageChangeObjet(Storage temporaryStorageChangeObjet) {
        this.temporaryStorageChangeObjet = temporaryStorageChangeObjet;
    }

    public Storage getTemporaryStorageChangeObjet() {
        return temporaryStorageChangeObjet;
    }
     
    
    public ArrayList<StorageItem> getAllItems()
    {
        return stoItemDb.getAllItems();
    }
}
